package mobi.smarthosts;

import java.util.Date;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;

public class SmartHosts extends Application {

    public static PendingIntent sAutoSyncSender = null;

    @Override
    public void onCreate() {
        Configuration.getInstance(this);

        Date nextUpdateTime = getNextCheckUpdateTime();
        Log.i("Next check update time is %s", nextUpdateTime.toString());
        Intent intent = new Intent(CheckingUpdateService.ACTION_CHECK_UPDATE);
        intent.setClass(this, CheckingUpdateService.class);
        sAutoSyncSender = PendingIntent.getService(this, 0, intent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.cancel(sAutoSyncSender); // cancel previous services alarm
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                nextUpdateTime.getTime(), Utils.DAY_MILLIS, sAutoSyncSender);
    }

    public static Date getNextCheckUpdateTime() {
        Date date = new Date();
        date.setDate(date.getDate()+1); // schedule to next day.
        return date;
    }

}
